﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.ComponentModel;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Resource;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.Log;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm;

namespace KMXMBillService
{
    [Description("科目余额明细表表单")]
    [Kingdee.BOS.Util.HotUpdate]
    public class KMMXBILL : AbstractDynamicFormPlugIn
    {
        public override void BeforeBindData(EventArgs e)
        {
            int i;

            string FSTARTYEAR = this.View.OpenParameter.GetCustomParameter("FSTARTYEAR").ToString();
            string FENDYEAR = this.View.OpenParameter.GetCustomParameter("FENDYEAR").ToString();
            string FSTARTPERIOD = this.View.OpenParameter.GetCustomParameter("FSTARTPERIOD").ToString();
            string FENDPERIOD = this.View.OpenParameter.GetCustomParameter("FENDPERIOD").ToString();
            string FCURRENCYID = this.View.OpenParameter.GetCustomParameter("FCURRENCYID").ToString();
            string FPOSTED = this.View.OpenParameter.GetCustomParameter("FPOSTED").ToString();
            string FFORBIDSTATUS = this.View.OpenParameter.GetCustomParameter("FFORBIDSTATUS").ToString();
            string FACCOUNTBOOKID = this.View.OpenParameter.GetCustomParameter("FACCOUNTBOOKID").ToString();
            string FACCTORGID = this.View.OpenParameter.GetCustomParameter("FACCTORGID").ToString();
            string FAcctNumber = this.View.OpenParameter.GetCustomParameter("FAcctNumber").ToString();
            string FAcctName = this.View.OpenParameter.GetCustomParameter("FAcctName").ToString();
            string FDepC = this.View.OpenParameter.GetCustomParameter("FDepC").ToString();
            string FDepNO = this.View.OpenParameter.GetCustomParameter("FDepNO").ToString();
            string FXMNO = this.View.OpenParameter.GetCustomParameter("FXMNO").ToString();
            string FPERIOD = this.View.OpenParameter.GetCustomParameter("FPERIOD").ToString();  
            string FACCTBOOKNAME = this.View.OpenParameter.GetCustomParameter("FACCTBOOKNAME").ToString();


            this.Model.SetValue("FACCTBOOKNAME", FACCTBOOKNAME);
            this.Model.SetValue("FPERIOD", FPERIOD);
            this.Model.SetValue("F_PAEZ_KM", FAcctNumber);
            this.Model.SetValue("F_PNVX_Text", FAcctName);

            string FSQL = "exec SP_MXXMBILL_SJ  '" + FSTARTYEAR + "' ,'" + FSTARTPERIOD + "','" + FENDYEAR + "','" + FENDPERIOD + "','" + FCURRENCYID + "','" + FPOSTED + "','" + FFORBIDSTATUS + "','" + FACCOUNTBOOKID + "','" + FACCTORGID + "','" + FAcctNumber + "','" + FDepC + "','" + FDepNO + "','" + FXMNO + "'";
            DataSet DSSK;
            DataTable DTSK;
            DSSK = DBUtils.ExecuteDataSet(this.Context, FSQL);
            DTSK = DSSK.Tables[0];
              if (DTSK.Rows.Count > 0)
              {
                  for (i = 0; i < DTSK.Rows.Count; i++)
                  {
                      this.Model.CreateNewEntryRow("F_PNVX_Entity");
                      this.Model.SetValue("F_PAEZ_FID", DTSK.Rows[i]["FID"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_FDate", DTSK.Rows[i]["FDate"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_PZZ", DTSK.Rows[i]["FVOUCHERGROUP"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_PZID", DTSK.Rows[i]["FVOUCHERGROUPNO"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_ZY", DTSK.Rows[i]["FEXPLANATION"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_HSNAME", DTSK.Rows[i]["FHSITEMNAME"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_De", DTSK.Rows[i]["FDeAmount"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_CRE", DTSK.Rows[i]["FCreAmount"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_DC", DTSK.Rows[i]["FDC"].ToString(), i);
                      this.Model.SetValue("F_PAEZ_YE", DTSK.Rows[i]["FYEAmount"].ToString(), i);
                  }
              }

              this.View.UpdateView("F_PNVX_Entityss");
        }

        public override void EntityRowDoubleClick(EntityRowClickEventArgs e)
        {
            base.EntityRowDoubleClick(e);
            int rowSeq = e.Row;     // 行序号，以1开始
            string fldKey = e.ColKey;
            string formId = string.Empty;
            if (e.ColKey.EqualsIgnoreCase("F_PAEZ_PZID") && rowSeq != -1)
            {
                   try
                   {
                    string FVOUCHERGROUPNO = this.Model.GetValue("F_PAEZ_PZID", rowSeq).ToString();
                    string FDate = this.Model.GetValue("F_PAEZ_FDate", rowSeq).ToString();
                    string FVOUCHERGROUPID = this.Model.GetValue("F_PAEZ_PZZ", rowSeq).ToString();
                    string FACCOUNTBOOKID = this.View.OpenParameter.GetCustomParameter("FACCOUNTBOOKID").ToString();
                    string FACCTORGID = this.View.OpenParameter.GetCustomParameter("FACCTORGID").ToString();

                    string FSQLB = "/*dialect*/ SELECT FVOUCHERID FROM T_GL_VOUCHER T1 " +
                                   "LEFT JOIN T_BD_VOUCHERGROUP_L T2 ON T1.FVOUCHERGROUPID=T2.FVCHGROUPID " +
                                   "WHERE T2.FNAME='" + FVOUCHERGROUPID + "' AND T1.FACCOUNTBOOKID =" + FACCOUNTBOOKID + " and t1.FDate ='" + FDate + "' " +
                                   "AND T1.FACCTORGID =" + FACCTORGID + " AND T1.FVOUCHERGROUPNO='" + FVOUCHERGROUPNO + "'";

                    string pkValue = DBUtils.ExecuteScalar<string>(this.Context, FSQLB, string.Empty, null);
                    formId = "GL_VOUCHER";

                    this.ShowForm(formId, pkValue);
                   }
                  catch(Exception exp)
                   {

                   }
            }
            if (string.IsNullOrWhiteSpace(formId))
            {
                // 双击的是普通单元格，不需要显示单据
                return;
            }
        }
        private void ShowForm(string formId, string pkValue)
        {
            // using Kingdee.BOS.Core.Bill;
            string pageId = Guid.NewGuid().ToString();
            BillShowParameter showParameter = new BillShowParameter();
            showParameter.FormId = "GL_VOUCHER";
            showParameter.OpenStyle.ShowType = ShowType.Floating;
            showParameter.PageId = pageId;
            showParameter.Status = OperationStatus.EDIT;
            showParameter.PKey = pkValue;
            this.View.ShowForm(showParameter);
        }
    }
}
    